<template>
  <!-- :model="formInline" -->
  <el-form :inline="true" class="demo-form-inline">
    <el-form-item label="一级分类">
      <el-select v-model="getCategory1Id" placeholder="请选择" size="small" @change="getCategory2Data">
        <el-option :label="item.name" :value="item.id" v-for="item in getCategory1" :key="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类">
      <el-select v-model="getCategory2Id" placeholder="请选择" size="small" @change="getCategory3Data">
        <el-option :label="item.name" :value="item.id" v-for="item in getCategory2" :key="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="getCategory3Id" placeholder="请选择" size="small" @change="storageCategory3Id">
          <el-option :label="item.name" :value="item.id" v-for="item in getCategory3" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form-item>

    <!-- <div>{{ getCategory1 }}</div>
    <div>{{ getCategory1Id }}</div> -->
  </el-form>
</template>

<script>
// 导入store仓库
import { mapActions, mapState, mapMutations } from 'vuex'
export default {
  name: 'ReclassIfy',
  data: () => {
    return {
      getCategory1Id: '', // 用户选择的一级分类id
      getCategory2Id: '', // 用户选择的二级分类id
      getCategory3Id: '' // 用户选择的三级分类id
    }
  },
  methods: {
    // 解构
    ...mapActions('reclassify', ['getCategory1List', 'getCategory2List', 'getCategory3List']),
    ...mapMutations('reclassify', ['CHANGE_GET_CATEGORY1_ID', 'CHANGE_GET_CATEGORY2_ID', 'CHANGE_GET_CATEGORY3_ID']),
    // 当一级分类发生变化的时候同时调用获取二级分类数据的actions方法
    getCategory2Data() {
      this.getCategory2Id = ''
      this.getCategory3Id = ''
      this.$emit('getAttrId', { categoryId: this.getCategory1Id, level: 1 })
      // 在当前一级分类选择之后,调用仓库中的 mutations 方法来进行对后续二级分类接口需要的id进行修改
      this.CHANGE_GET_CATEGORY1_ID(this.getCategory1Id)
      this.getCategory2List()
    },
    // 当二级分类发生变化的时候同时调用获取三级分类数据的actions方法
    getCategory3Data() {
      this.getCategory3Id = ''
      this.$emit('getAttrId', { categoryId: this.getCategory2Id, level: 2 })
      this.CHANGE_GET_CATEGORY2_ID(this.getCategory2Id)
      // 将收集好的数据通过参数来进行传递
      this.getCategory3List(this.getCategory2Id)
    },
    // 存储选择的三级id
    storageCategory3Id() {
      this.$emit('getAttrId', { categoryId: this.getCategory3Id, level: 3 })
      this.CHANGE_GET_CATEGORY3_ID(this.getCategory3Id)
    }
  },
  computed: {
    ...mapState('reclassify', ['getCategory1', 'getCategory2', 'getCategory3'])
  },
  mounted() {
    // 在挂载完成时进行获取到一级分类数据
    this.getCategory1List()
  }
}
</script>

<style lang="scss" scoped></style>
